package com.cat.recruit.controller;


import com.cat.recruit.bean.dto.UserDTO;
import com.cat.recruit.bean.request.AppointmentSlotRequest;
import com.cat.recruit.bean.request.UserApplyRequest;
import com.cat.recruit.common.result.Result;
import com.cat.recruit.config.JwtUserDetails;
import com.cat.recruit.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * @author cat
 */
@Tag(name = "用户模块")
@RestController
@RequestMapping(value = "api/wx/**", produces = "application/json;charset=UTF-8")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "用户登录", description = "通过传递code给微信api验证得到openId进行登录")
    @GetMapping("/login")
    public Result login(@RequestParam("code") String code){
        return userService.login(code);
    }

    @Operation(summary = "用户获得自己的信息", description = "通过openId获得自己注册的信息，包括名字，班级等")
    @GetMapping("/getInformation")
    public Result getInformation(@RequestParam("openId") String openId){
        return userService.getInformation(openId);
    }

    @Operation(summary = "用户预约面试", description = "通过前端提供openId和面试的id进行预约")
    @PostMapping("/interviews/appointments")
    public Result bookInterview
            (@RequestParam("appointmentId")Integer appointmentId,
             @RequestHeader("Authorization") String authHeader){
        String token = authHeader.replace("Bearer ", "");
        return  userService.bookInterview(appointmentId,token);
    }

    @Operation(summary = "取消预约面试", description = "用户通过预约ID取消预约")
    @PostMapping("/interviews/appointments/cancel")
    public Result cancelInterview(
            @RequestBody AppointmentSlotRequest  appointmentSlotRequest,
            @AuthenticationPrincipal JwtUserDetails userDetails) {
        return userService.cancelInterview(appointmentSlotRequest.getId(), userDetails,  appointmentSlotRequest.getAccessType());
    }

    @Operation(summary = "用户获得全部可以进行预约的面试信息", description = "用户获得全部可以进行预约的面试信息")
    @GetMapping("/interviews/getInterviewTime")
    public Result getInterviewTime(){
        return userService.getInterviewTime();
    }

    /**
     * 报名
     */
    @Operation(summary = "报名", description = "用户传递报名信息进行报名")
    @PostMapping("/apply")
    public Result submitScore(
            @RequestBody UserApplyRequest userApplyRequest,
            @AuthenticationPrincipal JwtUserDetails userDetails
    ){
        System.out.println("DTO对象: " + userApplyRequest);
        System.out.println("userName: " + userApplyRequest.getUserName());
        return userService.apply(
                userApplyRequest.getUserName(),
                userApplyRequest.getUserNumber(),
                userApplyRequest.getAcademy(),
                userApplyRequest.getDirection(),
                userApplyRequest.getPhone(),
                userApplyRequest.getEmail(),
                userApplyRequest.getSelfIntroduction(),
//                userDTO.getUsername(),
//                userDTO.getUserNumber(),
//                userDTO.getAcademy(),
//                userDTO.getDirection(),
//                userDTO.getPhoneNumber(),
//                userDTO.getEmail(),
//                userDTO.getUserIntro(),
                userDetails
        );
    }

    @Operation(summary = "获取预约状态", description = "获取预约状态")
    @PostMapping("/interviews/getAppointmentState")
    public Result getAppointmentState(@AuthenticationPrincipal JwtUserDetails userDetails){
        return userService.getAppointmentState(userDetails);
    }
}
